﻿; (function (window, undefined, $) {
    $(document).ready(function () {
        var gridopt = {
            url: options.listUrl,
            colModel: [
                    { display: '子项目', name: 'SubProject', hide: true },
                    { display: '放款状态', name: 'LoanStatus', hide: true },
                    { display: '满标状态', name: 'TenderStatus', hide: true },
                    //{ display: '编号', name: 'ProjectNo', width: "10%", sortable: true, hide: false, align: 'left', iskey: true },
                    { display: '项目名称', name: 'ProjectName', width: "30%", sortable: false, align: 'left' },
                    { display: '贷款总额', name: 'TotalLoan', width: "13%", sortable: false, align: 'left' },
                    { display: '年收益率', name: 'AnnualisedRates', width: "7%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '附送收益', name: 'AddAnnualisedRates', width: "5%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '项目位置', name: 'ProjectLocation', width: "10%", sortable: false, align: 'left' },
                    { display: '子项目', name: 'ID', width: "7%", sortable: false, align: 'center', process: processSubProject },
                    { display: '放款时间', name: 'LoanTime', width: "9%", sortable: false, align: 'left' },
                    { display: '操作', name: 'ID', width: "15%", sortable: false, align: 'center', process: processOp }
            ],
            sortname: "ID",
            sortorder: "ASC",
            title: false,
            rp: 15,
            usepager: true,
            showcheckbox: false
        };
        var xjgrid = new xjGrid("gridlist", gridopt);

        function processAnnualisedRates(value) {
            var ops = [];
            ops.push(Math.round(value * 100), "%");
            return ops.join("");
        }

        function processTotalLoan(value) {
            var ops = [];
            ops.push(formatNum(value));
            return ops.join("");
        }

        function formatNum(num) {
            if (!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)) { alert("wrong!"); return num; }
            var a = RegExp.$1, b = RegExp.$2, c = RegExp.$3;
            var re = new RegExp().compile("(\\d)(\\d{3})(,|$)");
            while (re.test(b)) b = b.replace(re, "$1,$2$3");
            return a + "" + b + "" + c;
        }

        function processSubProject(value, cell) {
            var ops = [];
            if (cell[0] == 1) {
                ops.push("&nbsp;<a title='子项目' href='javascript:;'  onclick=\"util.PetProject('", value, "')\"><i class='fa fa-edit' ></i>子项目</a>");
            }
            return ops.join("");
        }

        function processOp(value, cell) {
            var ops = [];
            if (cell[2] == 1) { //满标状态为1
                if (cell[1] == 0) { //放款状态为0
                    //ops.push("&nbsp;<a title='放款' href='javascript:;'  onclick=\"util.Loan('", value, "')\"><i class='fa fa-edit' ></i>放款</a>");
                    ops.push("&nbsp;<input type='button' id='myloan'  class='btn btn82 btn_search' value='放款' onclick=\"util.Loan('", value, "')\">");
                } else if (cell[1] == 1) {
                    ops.push("&nbsp;已放款");
                }
            }
            return ops.join("");
        }

        $("#formQuery").submit(function () {
            xjgrid.Query(this);
            return false;
        });
        $("#btnSearch").click(function () {
            $("#formQuery").submit();
        });
        

        util.PetProject = function (id) {
            var url = "PetProject.html?id=" + id;
            location.href = url;
        };

        util.Loan = function (id) {
            $("#myloan").attr("disabled", true);
            if (!confirm("您确定要执行放款操作吗？")) {
                $("#myloan").attr("disabled", false);
                return;
            }
            $.ajax({
                url: options.loanUrl,
                type: "Post",
                success: function (data) {
                    var obj = JSON.parse(data);
                    if (obj.errorCode == 200) {
                        alert(obj.errorMessage);
                        window.location.href = "/Admin/T_Project/ProjectAuditList";
                    }
                    else {
                        alert(obj.errorMessage);
                    }
                    $("#myloan").attr("disabled", false);
                },
                data: "id=" + id //这里没有参数可以不穿
            });
        };

    });

})(window, undefined, jQuery);